package org.example.contestb.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;

import org.example.contestb.entity.BO.LoginBO;
import org.example.contestb.entity.DTO.*;
import org.example.contestb.entity.Doctor;
import org.example.contestb.entity.VO.DoctorVO;
import org.example.contestb.responce.R;
import org.example.contestb.service.DoctorService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;
//@Slf4j
@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Resource
    private DoctorService doctorService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @SaCheckLogin
    @PostMapping("/register")
    public R register(@RequestBody @Validated RegisterDTO registerDTO){
        Doctor doctor = doctorService.Register(registerDTO);
        String userId = doctor.getUuid();
        StpUtil.login(userId);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        redisTemplate.opsForValue().set("TOKEN_" + userId,tokenInfo.getTokenValue(),1, TimeUnit.DAYS);
        DoctorVO doctorVO =DoctorVO.builder()
                .token(tokenInfo.getTokenValue())
                .username(doctor.getUsername())
                .email(doctor.getEmail())
                .build();
        return R.success(doctorVO);
    }

    @SaCheckLogin
    @PostMapping("/login")
    public R login(@RequestBody @Validated LoginDTO loginDTO, HttpServletResponse response){
        LoginBO loginBO = doctorService.login(loginDTO);
        String userId = loginBO.getUserId();
        Integer role = loginBO.getRole();
        StpUtil.login(userId);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        redisTemplate.opsForValue().set("TOKEN_" + userId, tokenInfo.getTokenValue(), 1, TimeUnit.DAYS);
        Cookie cookie = new Cookie("token", tokenInfo.getTokenValue());
        cookie.setHttpOnly(true);
        cookie.setSecure(false);
        cookie.setPath("/");
        cookie.setMaxAge((int) TimeUnit.DAYS.toSeconds(1));
        response.addCookie(cookie);
        response.addHeader("Set-Cookie", "token:" + tokenInfo.getTokenValue() + "; HttpOnly; SameSite=None; Secure");
        DoctorVO doctorVO = DoctorVO
                .builder()
                .token(tokenInfo.getTokenValue())
                .userId(userId)
                .role(role)
                .build();
        return R.success(doctorVO);
    }
    @SaCheckLogin
    @PostMapping("/send")
    public R sendCode(@RequestBody @Validated EmailDTO emailDTO){
        String email =  emailDTO.getEmail();
        doctorService.sendCode(email);
        return R.success("发送成功");
    }

    @SaCheckLogin
    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody @Validated UpdateDTO updateDTO){
        String formerPassword = updateDTO.getFormerPassword();
        String presentPassword = updateDTO.getPresentPassword();
        doctorService.updateCode(formerPassword,presentPassword);
        return R.success("修改成功");
    }

    @SaCheckLogin
    @GetMapping("/display")
    public R display(){
        return R.success(doctorService.display());
    }

    @SaCheckLogin
    @PostMapping("/logOut")
    public R logOut(){
        String userId = StpUtil.getLoginIdAsString();
        StpUtil.logout(userId);
        return R.success("退出成功！");
    }

    @SaCheckLogin
    @PostMapping("/contact")
    public R contactUs(@RequestBody @Validated MessageDTO messageDTO){
        return R.success(doctorService.contactUs(messageDTO));
    }
}
